package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Enumeration;

import com.corpcredit.Constants;
import com.corpcredit.model.Company;
import com.corpcredit.model.Hyfl;
import com.corpcredit.model.Jbqk;
import com.corpcredit.service.GenericManager;
import com.corpcredit.webapp.util.FacesUtils;

public class JbqkForm extends BasePage implements Serializable
{
    private GenericManager<Jbqk, Long> jbqkManager;
    private GenericManager<Hyfl, Long> hyflManager;
    private GenericManager<Company, Long> companyManager; 
    private Jbqk jbqk = new Jbqk();
    private Long id;
    private Long hyflId;

    public Long getHyflId()
    {
        if( id != null && id != 0 )
        {
            this.hyflId = jbqk.getHyfl().getId();
            log.debug( "======hyflid========"+ hyflId );
            return hyflId;
        }
        else
        {
            return null;
        }

    }

    public void setHyflId( Long hyflId )
    {
        this.hyflId = hyflId;
    }

    public void setJbqkManager( GenericManager<Jbqk, Long> jbqkManager )
    {
        this.jbqkManager = jbqkManager;
    }
    
    public void setHyflManager( GenericManager<Hyfl, Long> hyflManager )
    {
        this.hyflManager = hyflManager;
    }

    public void setCompanyManager( GenericManager<Company, Long> companyManager )
    {
        this.companyManager = companyManager;
    }

    public Jbqk getJbqk()
    {
        return jbqk;
    }

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setId( Long id )
    {
        this.id = id;
    }

    public String delete()
    {
        jbqkManager.remove( jbqk.getId() );
        addMessage( "jbqk.deleted" );

        return "list";
    }

    public String edit()
    {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not
        // with previous versions
        if( id != null && id != 0 )
        {
            jbqk = jbqkManager.get( id );
        }
        else
        {
            jbqk = new Jbqk();
        }
        FacesUtils.setManagedBeanInSession( Constants.JBQK, jbqk );
        return "edit";
    }

    public String save()
    {
        boolean isNew = (jbqk.getId() == null || jbqk.getId() == 0);
        if ( hyflId !=null && hyflId !=0) jbqk.setHyfl( hyflManager.get( hyflId ) );
        jbqkManager.save( jbqk );

        String key = (isNew) ? "jbqk.added" : "jbqk.updated";
        addMessage( key );

        if( isNew )
        {
            return "list";
        }
        else
        {
            return "edit";
        }
    }
}